from socket import *
import os
import json
import datetime
import shutil
import time
from common import *

def getIP(domain):
    myaddr = getaddrinfo(domain, 'http')
    return myaddr[0][4][0]

import socket
import os
import sys
import struct
import json
from socket import *
import time
from loguru import logger
import shutil

father_path_old = r'E:\project\DataBaseData\OldAnalyze'
father_path_new = r'E:\project\DataBaseData\AnalyzeData'

def socket_client(ip,hoos):
    try:
        s = socket(AF_INET, SOCK_STREAM)
        # print(ip,hoos)
        s.connect((ip, hoos))
    except Exception as msg1:
        logger.debug('connect wrong '+ str(msg1))
        print(msg1)
        sys.exit(1)
    logger.info(s.recv(1024))
    while True:
        dirls = ['AnalyzeDataNew1','AnalyzeDataNew2','AnalyzeDataNew3','AnalyzeDataNew4','AnalyzeDataNew5','AnalyzeDataRerun','Old1','Old2','Old3','Old4','Old5']
        for dirlsi in dirls:
            if 'AnalyzeData' in dirlsi:
                father_path_use = father_path_new
            else:
                father_path_use = father_path_old
            for json_data in os.listdir(os.path.join(father_path_use,dirlsi)):
                time.sleep(1)
                if json_data == 'back_up' or '.zip' not in json_data:
                    continue
                if not os.path.exists(os.path.join(father_path_use,dirlsi, json_data)):
                    continue
                FileType = dirlsi
                FileName = json_data
                zipkey = iscompletezip(os.path.join(father_path_use,dirlsi, json_data))
                if zipkey:
                    continue
                FileSize = os.path.getsize(os.path.join(father_path_use,dirlsi, FileName))
                FileDict = {'FileName': FileName, 'FileSize': FileSize, 'FileType': FileType}
                FileDict = json.dumps(FileDict, ensure_ascii=False).encode()
                try:
                    s.send(FileDict)
                    logger.info('start sending ,the file is {0}'.format(FileName))
                    with open(os.path.join(father_path_use,dirlsi, FileName), 'rb') as f:
                        while FileSize:
                            data = f.read(1024)
                            s.send(data)
                            FileSize -= len(data)
                    time.sleep(1)
                    a = s.recv(1024)
                    if a.decode() != 'get it wrong':
                        logger.info('start remove')
                        shutil.move(os.path.join(father_path_use,dirlsi, FileName), os.path.join(father_path_use,dirlsi, 'back_up', FileName))
                        if os.path.exists(os.path.join(father_path_use,dirlsi, FileName)):
                            shutil.rmtree(os.path.join(father_path_use,dirlsi, FileName))
                    else:
                        logger.info('receive wrong')
                    s.close()
                except Exception as e:
                    logger.debug('connect wrong1 '+ str(e))
                    while True:
                        try:
                            s.close()
                            s = socket(AF_INET, SOCK_STREAM)
                            s.connect((ip, hoos))
                            break
                        except Exception as msg:
                            logger.debug('connect wrong2 '+ str(msg))
                            continue
                while True:
                    try:
                        s.close()
                        s = socket(AF_INET, SOCK_STREAM)
                        s.connect((ip, hoos))
                        break
                    except Exception as msg:
                        logger.debug('connect wrong3 '+ str(msg))
                        continue
                logger.info(s.recv(1024))


if __name__ == '__main__':
    ip = '192.168.0.185'
    hoos = 8600
    socket_client(ip,hoos)